package com.controller;

import com.bean.*;
import com.mvc.ResponseBody;
import com.service.UserService;
import com.util.DateFormatUtil;
import com.util.JSONUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class UserController {
    UserService userService=new UserService();
    @ResponseBody("/user/console.do")
    public String console(HttpServletRequest request, HttpServletResponse response) {
        List<Map<String, Integer>> data = userService.console();
        Message msg = new Message();
        if (data.size() == 0) {
            msg.setStatus(-1);
        } else {
            msg.setStatus(0);
        }
        msg.setData(data);
        String json = JSONUtil.toJSON(msg);
        return json;
    }
    @ResponseBody("/user/list.do")
    public String list(HttpServletRequest request, HttpServletResponse response) {
        int offset = Integer.parseInt(request.getParameter("offset"));
        int pageNumber = Integer.parseInt(request.getParameter("pageNumber"));
        List<User> userList = userService.findAll(true, offset, pageNumber);
        List<BootStrapTableUser> newList = new ArrayList<>();
        for (User user : userList) {
            String regTime = DateFormatUtil.format(user.getRegTime());
            String lastLoginTime=user.getLastLoginTime()==null?"未曾登录":DateFormatUtil.format(user.getLastLoginTime());
            String password="******";
            BootStrapTableUser bootStrapTableUser = new BootStrapTableUser(user.getId(),user.getUserName(),user.getUserPhone(),password,regTime,lastLoginTime);
            newList.add(bootStrapTableUser);
        }
        List<Map<String, Integer>> console = userService.console();
        Integer total = console.get(0).get("data1_size");
        ResultData<BootStrapTableUser> data = new ResultData<>();
        data.setRows(newList);
        data.setTotal(total);
        String json = JSONUtil.toJSON(data);
        return json;
    }
    @ResponseBody("/user/insert.do")
    public String insert(HttpServletRequest request, HttpServletResponse response){
        String userName = request.getParameter("userName");
        String userPhone = request.getParameter("userPhone");
        String cardNumber = request.getParameter("cardNumber");
        String password = request.getParameter("password");
        User user=new User(userName,userPhone,cardNumber,password);
        boolean flag = userService.insert(user);
        Message msg = new Message();
        if (flag) {
            msg.setStatus(0);
            msg.setResult("用户新增成功！");
        } else {
            msg.setStatus(-1);
            msg.setResult("用户新增失败");
        }
        String json = JSONUtil.toJSON(msg);
        return json;

    }
    @ResponseBody("/user/find.do")
    public String find(HttpServletRequest request, HttpServletResponse response) {
        String userPhone = request.getParameter("userPhone");
        //System.out.println(userPhone);
        User user = userService.findByPhone(userPhone);
        //System.out.println(user.getUserName());
        //System.out.println(express.getUserName());
        Message msg = new Message();
        if (user == null) {
            msg.setStatus(-1);
            msg.setResult("手机号不存在");
        } else {
            msg.setStatus(0);
            msg.setResult("查询成功");
            msg.setData(user);
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }
    @ResponseBody("/user/update.do")
    public String update(HttpServletRequest request, HttpServletResponse response) {
        int id = Integer.parseInt(request.getParameter("id"));
        String userName = request.getParameter("userName");
        String userPhone = request.getParameter("userPhone");
        String cardNumber = request.getParameter("cardNumber");
        String password=request.getParameter("password");
        User user=new User();
        user.setUserName(userName);
        user.setUserPhone(userPhone);
        user.setCardNumber(cardNumber);
        user.setPassword(password);
        boolean flag = userService.update(id, user);
        Message msg = new Message();
        if (flag) {
            msg.setStatus(0);
            msg.setResult("修改成功");
        } else {
            msg.setStatus(-1);
            msg.setResult("修改失败");
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }
    @ResponseBody("/user/delete.do")
    public String delete(HttpServletRequest request, HttpServletResponse response) {
        int id = Integer.parseInt(request.getParameter("id"));
        boolean flag = userService.delete(id);
        Message msg = new Message();
        if (flag) {
            msg.setStatus(0);
            msg.setResult("删除成功");
        } else {
            msg.setStatus(-1);
            msg.setResult("删除失败");
        }
        String json = JSONUtil.toJSON(msg);
        return json;

    }
}
